home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / mobile / fma-2.0-stable-setup.exe / {app} / source / uSelectContact.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2005-01-26  |  13.0 KB  |  449 lines

  1. unit uSelectContact;
  2.  
  3. {
  4. *******************************************************************************
  5. * Descriptions: Selecting Multiple Contacts
  6. * $Source: /cvsroot/fma/fma/uSelectContact.pas,v $
  7. * $Locker:  $
  8. *
  9. * Todo:
  10. *
  11. * Change Log:
  12. * $Log: uSelectContact.pas,v $
  13. * Revision 1.10.6.1  2005/01/25 16:03:20  z_stoichev
  14. * Merged with 2.1 Beta 1 bugfixes
  15. *
  16. * Revision 1.10  2004/06/29 11:51:15  z_stoichev
  17. * Added Select One Contact support
  18. *
  19. * Revision 1.9  2004/06/19 11:16:06  z_stoichev
  20. * - Fixed Select contacts phone lookup.
  21. * - Changed Contact Group icons.
  22. *
  23. * Revision 1.8  2004/05/19 18:34:16  z_stoichev
  24. * Build 0.1.0.35c
  25. *
  26. * Revision 1.7  2004/04/01 15:05:49  z_stoichev
  27. * GUI changes
  28. * support for unknown contacts
  29. *
  30. * Revision 1.6  2003/12/16 17:38:38  z_stoichev
  31. * Add support for contacts without numbers.
  32. *
  33. * Revision 1.5  2003/12/01 12:21:28  z_stoichev
  34. * Add filter for mobile numbers only.
  35. *
  36. * Revision 1.4  2003/11/28 09:38:07  z_stoichev
  37. * Merged with branch-release-1-1 (Fma 0.10.28c)
  38. *
  39. * Revision 1.3.2.4  2003/11/10 14:03:10  z_stoichev
  40. * RC3
  41. *
  42. * Revision 1.3.2.3  2003/10/30 15:17:18  z_stoichev
  43. * Remove dublicated contacts issued from Groups.
  44. *
  45. * Revision 1.3.2.2  2003/10/29 16:22:42  z_stoichev
  46. * Add Groups support.
  47. *
  48. * Revision 1.3.2.1  2003/10/27 07:22:54  z_stoichev
  49. * Build 0.1.0 RC1 Initial Checkin.
  50. *
  51. * Revision 1.3  2003/10/21 08:51:52  z_stoichev
  52. * Added SelContacts property containing the
  53. * selected recipients addresses in format
  54. * "name [number]; ..."
  55. *
  56. * Revision 1.2  2003/10/15 16:14:17  z_stoichev
  57. * Select contacts by row, not by name.
  58. * Corrected some typos.
  59. * Added Clear button to delete all recipients.
  60. * Added header comments.
  61. *
  62. *
  63. *
  64. *******************************************************************************
  65. }
  66.  
  67. interface
  68.  
  69. uses
  70.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  71.   Dialogs, ExtCtrls, StdCtrls, ComCtrls, TntComCtrls, TntStdCtrls, ImgList,
  72.   Menus, Placemnt;
  73.  
  74. type
  75.   TfrmSelContact = class(TForm)
  76.     Label1: TLabel;
  77.     TntListView1: TTntListView;
  78.     AddButton: TButton;
  79.     SelectedList: TTntListView;
  80.     Label2: TLabel;
  81.     RemButton: TButton;
  82.     OkButton: TButton;
  83.     CancelButton: TButton;
  84.     TntEdit1: TTntEdit;
  85.     Label3: TLabel;
  86.     ComboBox1: TComboBox;
  87.     ClearButton: TButton;
  88.     ImageList1: TImageList;
  89.     CheckBox1: TCheckBox;
  90.     PopupMenu1: TPopupMenu;
  91.     AddContact1: TMenuItem;
  92.     N1: TMenuItem;
  93.     Properties1: TMenuItem;
  94.     FormStorage1: TFormStorage;
  95.     procedure TntListView1SelectItem(Sender: TObject; Item: TListItem;
  96.       Selected: Boolean);
  97.     procedure SelectedListSelectItem(Sender: TObject; Item: TListItem;
  98.       Selected: Boolean);
  99.     procedure RemButtonClick(Sender: TObject);
  100.     procedure AddButtonClick(Sender: TObject);
  101.     procedure TntEdit1Change(Sender: TObject);
  102.     procedure TntListView1DblClick(Sender: TObject);
  103.     procedure ClearButtonClick(Sender: TObject);
  104.     procedure TntListView1Insert(Sender: TObject; Item: TListItem);
  105.     procedure Properties1Click(Sender: TObject);
  106.     procedure AddContact1Click(Sender: TObject);
  107.     procedure FormShow(Sender: TObject);
  108.   private
  109.     { Private declarations }
  110.     procedure AddContact(Node: TTntListItem);
  111.     procedure UpdateContacts(Mask: string = '');
  112.     procedure InitContacts;
  113.     function get_Sel: string;
  114.     procedure set_Sel(const Value: string); 
  115.     function Get_MultiSelect: boolean;
  116.     procedure Set_MultiSelect(const Value: boolean);
  117.   public
  118.     { Public declarations }
  119.     property AllowMultiSelect: boolean read Get_MultiSelect write Set_MultiSelect;
  120.     property SelContacts: string read get_Sel write set_Sel;
  121.   end;
  122.  
  123. var
  124.   frmSelContact: TfrmSelContact;
  125.  
  126. implementation
  127.  
  128. uses
  129.   uSyncPhonebook, uMissedCalls, Unit1, uSIMEdit;
  130.  
  131. {$R *.dfm}
  132.  
  133. procedure TfrmSelContact.TntListView1SelectItem(Sender: TObject;
  134.   Item: TListItem; Selected: Boolean);
  135. begin
  136.   AddButton.Enabled := (TntListView1.SelCount <> 0) and
  137.     (AllowMultiSelect or (SelectedList.Items.Count = 0));
  138.   Properties1.Enabled := (TntListView1.SelCount = 1) and
  139.     (TntListView1.Selected.ImageIndex = 0); // only for contacts
  140. end;
  141.  
  142. procedure TfrmSelContact.SelectedListSelectItem(Sender: TObject;
  143.   Item: TListItem; Selected: Boolean);
  144. begin
  145.   RemButton.Enabled := Selected;
  146. end;
  147.  
  148. procedure TfrmSelContact.RemButtonClick(Sender: TObject);
  149. var
  150.   i: integer;
  151. begin
  152.   for i := SelectedList.Items.Count-1 downto 0 do
  153.     if SelectedList.Items[i].Selected then
  154.       SelectedList.Items[i].Delete;
  155. end;
  156.  
  157. procedure TfrmSelContact.AddButtonClick(Sender: TObject);
  158. var
  159.   i: integer;
  160. begin
  161.   for i := 0 to TntListView1.Items.Count-1 do
  162.     if TntListView1.Items[i].Selected then
  163.       AddContact(TntListView1.Items[i]);
  164. end;
  165.  
  166. procedure TfrmSelContact.TntEdit1Change(Sender: TObject);
  167. begin
  168.   CheckBox1.Enabled := ComboBox1.ItemIndex = 0;
  169.   UpdateContacts(TntEdit1.Text);
  170. end;
  171.  
  172. procedure TfrmSelContact.UpdateContacts(Mask: string);
  173. var
  174.   root: TtntTreeNode;
  175.   i,j: integer;
  176.   nme,tel: WideString;
  177. begin
  178.   case ComboBox1.ItemIndex of
  179.     0: root := Form1.FNodeContactsME;
  180.     1: root := Form1.FNodeContactsSM;
  181.     else exit; // just in case
  182.   end;
  183.   Mask := WideUpperCase(Mask);
  184.   TntListView1.Items.BeginUpdate;
  185.   try
  186.     TntListView1.Items.Clear;
  187.     if AllowMultiSelect and (ComboBox1.ItemIndex = 0) then
  188.       for i := 0 to Form1.FNodeGroups.Count-1 do begin
  189.       nme := Form1.FNodeGroups.Item[i].Text;
  190.       if (Mask = '') or (Pos(Mask,WideUpperCase(nme)) <> 0) then begin
  191.         tel := '';
  192.         for j := 0 to Form1.FNodeGroups.Item[i].Count-1 do begin
  193.           if length(tel) <> 0 then tel := tel + ',';
  194.           tel := tel + Form1.FNodeGroups.Item[i].Item[j].Text;
  195.         end;
  196.         with TntListView1.Items.Add do begin
  197.           Caption := nme;
  198.           SubItems.Add(tel);
  199.           ImageIndex := 1;
  200.           //StateIndex := -1;
  201.         end;
  202.       end;
  203.     end;
  204.     for i := 0 to root.Count-1 do begin
  205.       nme := root.Item[i].Text;
  206.       if (Mask = '') or (Pos(Mask,WideUpperCase(nme)) <> 0) then
  207.         for j := 0 to root.Item[i].Count-1 do
  208.           if not CheckBox1.Enabled or not CheckBox1.Checked or
  209.             (root.Item[i].Item[j].ImageIndex = 10) then
  210.             begin
  211.               tel := root.Item[i].Item[j].Text;
  212.               with TntListView1.Items.Add do begin
  213.                 Caption := nme;
  214.                 SubItems.Add(tel);
  215.                 ImageIndex := 0;
  216.                 StateIndex := root.Item[i].Item[j].ImageIndex;
  217.               end;
  218.             end;
  219.     end;
  220.   finally
  221.     TntListView1.Items.EndUpdate;
  222.   end;
  223. end;
  224.  
  225. procedure TfrmSelContact.TntListView1DblClick(Sender: TObject);
  226. begin
  227.   if TntListView1.Selected <> nil then AddButton.Click;
  228. end;
  229.  
  230. procedure TfrmSelContact.ClearButtonClick(Sender: TObject);
  231. begin
  232.   SelectedList.Clear;
  233. end;
  234.  
  235. function TfrmSelContact.get_Sel: string;
  236. var
  237.   i: integer;
  238. begin
  239.   Result := '';
  240.   for i := 0 to SelectedList.Items.Count-1 do begin
  241.     if Result <> '' then Result := Result + '; ';
  242.     if SelectedList.Items[i].ImageIndex = 0 then
  243.       if SelectedList.Items[i].Caption <> '' then
  244.         Result := Result + SelectedList.Items[i].Caption + ' [' + SelectedList.Items[i].SubItems[0] + ']'
  245.       else
  246.         Result := Result + SelectedList.Items[i].SubItems[0]
  247.     else
  248.       Result := Result + SelectedList.Items[i].Caption + ' {' + SelectedList.Items[i].SubItems[0] + '}'
  249.   end;
  250. end;
  251.  
  252. procedure TfrmSelContact.set_Sel(const Value: string);
  253. var
  254.   i,ptype: integer;
  255.   d,s,nme,num: string;
  256.   IsGroup: boolean;
  257.   procedure LookupPerson(phone: string; var cname: string; var ptype: integer);
  258.   var
  259.     i: integer;
  260.   begin
  261.     for i := 0 to TntListView1.Items.Count-1 do
  262.       // TODO: Add partial number matching
  263.       if WideCompareText(TntListView1.Items[i].SubItems[0],phone) = 0 then begin
  264.         cname := TntListView1.Items[i].Caption;
  265.         ptype := TntListView1.Items[i].StateIndex;
  266.         break;
  267.       end;
  268.   end;
  269. begin
  270.   InitContacts;
  271.   SelectedList.Clear;
  272.   d := Trim(Value);
  273.   repeat
  274.     nme := '';
  275.     ptype := 13;
  276.     i := Pos(';',d);
  277.     if i = 0 then i := Length(d)+1;
  278.     s := Trim(Copy(d,1,i-1));
  279.     Delete(d,1,i);
  280.     IsGroup := False;
  281.     if s <> '' then begin
  282.       { is it a group? }
  283.       if s[length(s)] = '}' then begin
  284.         IsGroup := True;
  285.         Delete(s,length(s),1);
  286.         i := Pos('{',s);
  287.         if i <> 0 then begin
  288.           nme := Trim(Copy(s,1,i-1));
  289.           Delete(s,1,i);
  290.         end;
  291.       end
  292.       else begin
  293.         if s[length(s)] = ']' then Delete(s,length(s),1);
  294.         i := Pos('[',s);
  295.         { ignore name, we'll lookup for it from phonebook }
  296.         if i <> 0 then Delete(s,1,i);
  297.         if s <> '' then begin
  298.           num := Form1.LookupNumber(s);  // if we have name instead of number,
  299.           if num <> '' then s := num;    // swap them...
  300.           LookupPerson(s,nme,ptype);
  301.         end;
  302.       end;
  303.     end;
  304.     if s <> '' then
  305.       with SelectedList.Items.Add do begin
  306.         if nme = '' then nme := Form1.LookupContact(s,sUnknownContact);
  307.         Caption := nme;
  308.         SubItems.Add(s);
  309.         if IsGroup then
  310.           ImageIndex := 1
  311.         else
  312.           StateIndex := ptype;
  313.         if not AllowMultiSelect then break;
  314.       end;
  315.   until d = '';
  316. end;
  317.  
  318. procedure TfrmSelContact.TntListView1Insert(Sender: TObject;
  319.   Item: TListItem);
  320. begin
  321.   Item.ImageIndex := 0;
  322. end;
  323.  
  324. procedure TfrmSelContact.Properties1Click(Sender: TObject);
  325. var
  326.   ContactME: PContactData;
  327.   ContactSM: PSIMData;
  328.   b: boolean;
  329. begin
  330.   b := False;
  331.   case ComboBox1.ItemIndex of
  332.     0: begin // Phonebook
  333.          if Form1.frmSyncPhonebook.FindContact(TntListView1.Selected.Caption,ContactME) then
  334.             with Form1.frmSyncPhonebook do begin
  335.               SelContact := ContactME;
  336.               b := DoEdit;
  337.             end;
  338.        end;
  339.     1: begin // SIM
  340.          if Form1.frmSIMEdit.FindContact(TntListView1.Selected.Caption,ContactSM) then
  341.             with Form1.frmSIMEdit do begin
  342.               SelContact := ContactSM;
  343.               b := DoEdit;
  344.             end;
  345.        end;
  346.   end;
  347.   if b then UpdateContacts(TntEdit1.Text);
  348. end;
  349.  
  350. procedure TfrmSelContact.AddContact1Click(Sender: TObject);
  351. var
  352.   b: boolean;
  353. begin
  354.   b := False;
  355.   case ComboBox1.ItemIndex of
  356.     0: b := Form1.frmSyncPhonebook.DoEdit(True); // Phonebook
  357.     1: b := Form1.frmSIMEdit.DoEdit(True);       // SIM
  358.   end;
  359.   if b then UpdateContacts(TntEdit1.Text);
  360. end;
  361.  
  362. procedure TfrmSelContact.AddContact(Node: TTntListItem);
  363. var
  364.   i: integer;
  365.   function RemoveDub: boolean;
  366.   var
  367.     i,j,k: integer;
  368.     sl: TStringList;
  369.     Data: PContactData;
  370.     s: string;
  371.   begin
  372.     Result := False;
  373.     try
  374.       sl := TStringList.Create;
  375.       try
  376.         for i := 0 to SelectedList.Items.Count-1 do
  377.           { Find groups }
  378.           if SelectedList.Items[i].ImageIndex = 1 then begin
  379.             { Parse all members }
  380.             sl.CommaText := SelectedList.Items[i].SubItems[0];
  381.             { Replace names with default numbers }
  382.             for j := 0 to sl.Count-1 do begin
  383.               Form1.frmSyncPhonebook.FindContact(sl[j],Data);
  384.               s := GetContactDefPhone(Data);
  385.               sl[j] := s;
  386.             end;
  387.             { Find numbers and remove dubs }
  388.             for j := 0 to sl.Count-1 do
  389.               for k := 0 to SelectedList.Items.Count-1 do
  390.                 if sl[j] = SelectedList.Items[k].SubItems[0] then begin
  391.                   { Dublicate, so delete it }
  392.                   SelectedList.Items[k].Delete;
  393.                   Result := True;
  394.                   Abort; // list is modified, we have to cancel loops now
  395.                 end;
  396.           end;
  397.       finally
  398.         sl.Free;
  399.       end;
  400.     except
  401.     end;  
  402.   end;
  403. begin
  404.   { first check if contact is already added }
  405.   for i := 0 to SelectedList.Items.Count-1 do
  406.     if SelectedList.Items[i].ImageIndex = Node.ImageIndex then begin
  407.       if (SelectedList.Items[i].Caption = Node.Caption) and
  408.         (SelectedList.Items[i].SubItems[0] = Node.SubItems[0]) then
  409.         exit;
  410.     end;
  411.   { ok, add it }
  412.   with SelectedList.Items.Add do begin
  413.     Caption := Node.Caption;
  414.     SubItems.Add(Node.SubItems[0]);
  415.     ImageIndex := Node.ImageIndex;
  416.     StateIndex := Node.StateIndex;
  417.   end;
  418.   { Remove dublicates (Groups + Contact) }
  419.   while RemoveDub do ;
  420. end;
  421.  
  422. procedure TfrmSelContact.FormShow(Sender: TObject);
  423. begin
  424.   InitContacts;
  425. end;
  426.  
  427. procedure TfrmSelContact.InitContacts;
  428. begin
  429.   if TntListView1.Items.Count = 0 then
  430.     FormStorage1.RestoreFormPlacement;
  431.   if TntListView1.Items.Count = 0 then
  432.     TntEdit1Change(nil);
  433. end;
  434.  
  435. function TfrmSelContact.Get_MultiSelect: boolean;
  436. begin
  437.   Result := TntListView1.MultiSelect;
  438. end;
  439.  
  440. procedure TfrmSelContact.Set_MultiSelect(const Value: boolean);
  441. begin
  442.   TntListView1.MultiSelect := Value;
  443.   if not Value then
  444.     while SelectedList.Items.Count > 1 do
  445.       SelectedList.Items.Delete(1);
  446. end;
  447.  
  448. end.
  449.